package com.wzt.lianfirecontrol.adapter.yighuan;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.function.yinhuan.YingHuanRecordModel;
import com.wzt.lianfirecontrol.bean.recode.user.NamePhoneModel;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.Utils;

import java.util.List;

public class YingHuanRecordAdapter extends BaseRecyclerAdapter<YingHuanRecordModel> {

    private Context context;

    private CallBack callBack;

    public YingHuanRecordAdapter(Context context, CallBack callBack) {
        this.context = context;
        this.callBack = callBack;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_yinghuan_record, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, YingHuanRecordModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_person_name;
        private TextView tv_person_phone;
        private LinearLayout ll_other_user;
        private TextView tv_device_date;
        private TextView tv_error_content;
        private View include_play_photo_three;
        private RelativeLayout rl_device_photo_1;
        private ImageView iv_device_photo_1;
        private RelativeLayout rl_device_photo_2;
        private ImageView iv_device_photo_2;
        private RelativeLayout rl_device_photo_3;
        private ImageView iv_device_photo_3;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView){
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_person_name = itemView.findViewById(R.id.tv_person_name);
        myViewHolder.tv_person_phone = itemView.findViewById(R.id.tv_person_phone);
        myViewHolder.ll_other_user = itemView.findViewById(R.id.ll_other_user);
        myViewHolder.tv_device_date = itemView.findViewById(R.id.tv_device_date);
        myViewHolder.tv_error_content = itemView.findViewById(R.id.tv_error_content);
        myViewHolder.include_play_photo_three = itemView.findViewById(R.id.include_play_photo_three);
        myViewHolder.rl_device_photo_1 = itemView.findViewById(R.id.rl_device_photo_1);
        myViewHolder.iv_device_photo_1 = itemView.findViewById(R.id.iv_device_photo_1);
        myViewHolder.rl_device_photo_2 = itemView.findViewById(R.id.rl_device_photo_2);
        myViewHolder.iv_device_photo_2 = itemView.findViewById(R.id.iv_device_photo_2);
        myViewHolder.rl_device_photo_3 = itemView.findViewById(R.id.rl_device_photo_3);
        myViewHolder.iv_device_photo_3 = itemView.findViewById(R.id.iv_device_photo_3);

        int width = (ScreenUtils.getScreenWidth(context) - Utils.dip2px(context, 90)) / 3;
        myViewHolder.rl_device_photo_1.getLayoutParams().width = width;
        myViewHolder.rl_device_photo_1.getLayoutParams().height = width;
        myViewHolder.rl_device_photo_2.getLayoutParams().width = width;
        myViewHolder.rl_device_photo_2.getLayoutParams().height = width;
        myViewHolder.rl_device_photo_3.getLayoutParams().width = width;
        myViewHolder.rl_device_photo_3.getLayoutParams().height = width;
    }

    private void setItemData(MyViewHolder myViewHolder, YingHuanRecordModel yingHuanRecordModel, int position){
        ViewGroup.LayoutParams layoutParams = myViewHolder.ll_item_content.getLayoutParams();
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(context.getResources().getColor(R.color.white));
        if (position == 0){
            myViewHolder.ll_item_content.setPadding(Utils.dip2px(context, 15), 0,
                    Utils.dip2px(context, 15),Utils.dip2px(context, 15));
        }else {
            myViewHolder.ll_item_content.setPadding(Utils.dip2px(context, 15), Utils.dip2px(context, 15),
                    Utils.dip2px(context, 15),Utils.dip2px(context, 15));
        }
        if (position == getmDatas().size() - 1){
            gradientDrawable.setCornerRadii(new float[]{ 0, 0, 0, 0, Utils.dip2px(context, 6), Utils.dip2px(context, 6),
                    Utils.dip2px(context, 6), Utils.dip2px(context, 6)});
        }else {
            gradientDrawable.setCornerRadii(new float[]{0, 0, 0, 0, 0, 0, 0, 0});
        }
        myViewHolder.ll_item_content.setBackground(gradientDrawable);
        List<NamePhoneModel> namePhoneModels = yingHuanRecordModel.getUserList();
        myViewHolder.tv_person_name.setText(namePhoneModels.get(0).getName());
        myViewHolder.tv_person_phone.setText(namePhoneModels.get(0).getPhone());
        // 其他整改人员
        if (namePhoneModels.size() < 2) {
            myViewHolder.ll_other_user.setVisibility(View.GONE);
        } else {
            try {
                myViewHolder.ll_other_user.setVisibility(View.VISIBLE);
                myViewHolder.ll_other_user.removeAllViews();
                for (int i = 1; i < namePhoneModels.size(); i++) {
                    String userName = namePhoneModels.get(i).getName();
                    String userPhone = namePhoneModels.get(i).getPhone();
                    View view = LayoutInflater.from(context).inflate(R.layout.item_other_user, myViewHolder.ll_other_user, false);
                    TextView tv_person_name = view.findViewById(R.id.tv_person_name);
                    tv_person_name.setText(userName);
                    TextView tv_person_phone = view.findViewById(R.id.tv_person_phone);
                    tv_person_phone.setText(userPhone);
                    myViewHolder.ll_other_user.addView(view);
                }
            } catch (Exception e) {
                myViewHolder.ll_other_user.setVisibility(View.GONE);
            }
        }


        myViewHolder.tv_device_date.setText(yingHuanRecordModel.getRectificationTimeStr());
        myViewHolder.tv_error_content.setText(yingHuanRecordModel.getRemark());

        String[] imgStrs = yingHuanRecordModel.getImgUrls().split(",");

        if (imgStrs.length < 1){
            myViewHolder.rl_device_photo_1.setVisibility(View.INVISIBLE);
        }else {
            myViewHolder.rl_device_photo_1.setVisibility(View.VISIBLE);
            GildeUtils.loadImageCorner(context, myViewHolder.iv_device_photo_1, imgStrs[0]);
            myViewHolder.iv_device_photo_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.showImage(imgStrs[0]);
                }
            });
        }
        if (imgStrs.length < 2){
            myViewHolder.rl_device_photo_2.setVisibility(View.INVISIBLE);
        }else {
            myViewHolder.rl_device_photo_2.setVisibility(View.VISIBLE);
            GildeUtils.loadImageCorner(context, myViewHolder.iv_device_photo_2,  imgStrs[1]);
            myViewHolder.iv_device_photo_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.showImage( imgStrs[1]);
                }
            });
        }
        if (imgStrs.length < 3){
            myViewHolder.rl_device_photo_3.setVisibility(View.INVISIBLE);
        }else {
            myViewHolder.rl_device_photo_3.setVisibility(View.VISIBLE);
            GildeUtils.loadImageCorner(context, myViewHolder.iv_device_photo_3, imgStrs[2]);
            myViewHolder.iv_device_photo_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callBack.showImage(imgStrs[2]);
                }
            });
        }
    }

    public interface CallBack{
        void showImage(String imgUrl);
    }
}
